package cn.ultrarisk.scadmin.manager.biz.solr.dao;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.solr.context.SolrState;
import cn.ultrarisk.scadmin.manager.biz.solr.dto.CollectionOverview;
import cn.ultrarisk.scadmin.manager.biz.solr.dto.ReplicaOverview;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrNode;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrReplica;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import cn.ultrarisk.scadmin.manager.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * solr集合节点数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class SolrReplicaDAO extends BaseDAO<SolrReplica, Integer> {
    @Autowired
    private SolrNodeDAO solrNodeDAO;

    /**
     * 构造器
     */
    public SolrReplicaDAO() {
        super(SolrReplica.class, SolrReplica.COLUMN_PROPERTY_MAP);
    }

    public Map<Integer, String> getNameMap(Collection<Integer> ids, boolean fullName) {
        Map<Integer, String> nameMap = new HashMap<Integer, String>();
        if (CollectionUtil.isNotEmpty(ids)) {
            List<SolrReplica> replicas = findByIds(ids, SolrReplica.ID, SolrReplica.CORE_NAME, SolrReplica.NODE_ID);
            if (CollectionUtil.isNotEmpty(replicas)) {
                for (SolrReplica replica : replicas) {
                    nameMap.put(replica.getId(), replica.getCoreName());
                }

                if (fullName) {
                    Map<Integer, Integer> replicaIdMap = new HashMap<Integer, Integer>();
                    List<Integer> nodeIds = new ArrayList<Integer>();
                    for (SolrReplica replica : replicas) {
                        nodeIds.add(replica.getNodeId());
                        replicaIdMap.put(replica.getId(), replica.getNodeId());
                    }
                    Map<Integer, SolrNode> nodeMap = solrNodeDAO.findMap(Query.inQuery(SolrNode.ID, nodeIds), SolrNode.ID, SolrNode.NAME);
                    for (Integer replicaId : nameMap.keySet()) {
                        nameMap.put(replicaId, nodeMap.get(replicaIdMap.get(replicaId)).getName() + "_" + nameMap.get(replicaId));
                    }
                }
            }
        }
        return nameMap;
    }

    public void formatCoreName(List<SolrReplica> replicas) {
        if (CollectionUtil.isNotEmpty(replicas)) {
            List<Integer> replicaIds = new ArrayList<Integer>();
            for (SolrReplica replica : replicas) {
                replicaIds.add(replica.getId());
            }
            Map<Integer, String> nameMap = getNameMap(replicaIds, true);
            for (SolrReplica replica : replicas) {
                replica.setCoreName(nameMap.get(replica.getId()));
            }
        }
    }

    public List<SolrReplica> findAvailable(Integer nodeId, Integer collectionId, boolean fullName) {
        Query query = Query.query(SolrReplica.AVAILABLE, true).eq(SolrReplica.NODE_ID, nodeId).eq(SolrReplica.COLLECTION_ID, collectionId);
        List<SolrReplica> replicas = findList(query);
        if (fullName) {
            formatCoreName(replicas);
        }
        return replicas;
    }

    public Page<SolrReplica> findPage(String host, Integer collectionId, SolrState state, int pageNo, int pageSize) {
        List<Integer> nodeIds = solrNodeDAO.findColumn(Query.query(SolrNode.HOST, host), SolrNode.ID, true, true);
        Query query = Query.query(SolrReplica.STATE, state).in(SolrReplica.NODE_ID, nodeIds).eq(SolrReplica.COLLECTION_ID, collectionId);
        Page<SolrReplica> paged = findPage(query, pageNo, pageSize);
        formatCoreName(paged.getList());
        return paged;
    }

    public CollectionOverview getCollectionOverview(Integer collectionId) {
        return sqlSessionTemplate.selectOne(entityName + ".getCollectionOverview", CollectionUtil.asList(collectionId));
    }

    public List<ReplicaOverview> getReplicaOverviews(Integer collectionId) {
        List<ReplicaOverview> overviews = sqlSessionTemplate.selectList(entityName + ".getReplicaOverview", CollectionUtil.asList(collectionId));
        List<Integer> replicaIds = new ArrayList<Integer>();
        for (ReplicaOverview overview : overviews) {
            replicaIds.add(overview.getId());
        }
        Map<Integer, String> nameMap = getNameMap(replicaIds, true);
        for (ReplicaOverview overview : overviews) {
            overview.setName(nameMap.get(overview.getId()));
        }
        return overviews;
    }

    public String getSelectUrl(Integer id) {
        SolrReplica replica = get(id);
        SolrNode node = solrNodeDAO.get(replica.getNodeId());
        return node.getBaseUrl() + "/" + replica.getCoreName() + "/select";
    }

    public String getUpdateUrl(Integer id) {
        SolrReplica replica = get(id);
        SolrNode node = solrNodeDAO.get(replica.getNodeId());
        return node.getBaseUrl() + "/" + replica.getCoreName() + "/update";
    }

    public String getAnalysisUrl(Integer id) {
        SolrReplica replica = get(id);
        SolrNode node = solrNodeDAO.get(replica.getNodeId());
        return node.getBaseUrl() + "/" + replica.getCoreName() + "/analysis/field";
    }
}
